#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Currency Converter Copyright (C) 2009  Chris Marsh
#
# Currency Converter is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# *********************************************************************************
#
# qcurrency.py, This module represents the gui of the main window.
#
# *********************************************************************************

import platform
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from converter_gui import ConverterGui
from options_dlg import OptionState,  OptionsDialog
from about_dlg import AboutDialog

__version__ = "0.1.0"
    
# *********************************************************************************

class MainWindow(QMainWindow):
    
    def __init__(self, parent=None):
        super(MainWindow,self).__init__(parent)
        
        size = QSize(550,  250)
        self.setMinimumSize(size)
        
        optionsAction = QAction('&Options',  self)
        optionsAction.setStatusTip('About Currency Converter')
        optionsAction.setIcon(QIcon(":/options.png"))
        self.connect(optionsAction, SIGNAL("triggered()"), self.showOptions)
        
        aboutAction = QAction('&About',  self)
        aboutAction.setStatusTip('About Currency Converter')
        self.connect(aboutAction, SIGNAL("triggered()"), self.showAbout)
        
        aboutQtAction = QAction(self.tr("About &Qt"), self)
        aboutQtAction.setStatusTip(self.tr("Show the Qt library's About box"))
        self.connect(aboutQtAction, SIGNAL("triggered()"), qApp, SLOT("aboutQt()"))
        
        quitAction = QAction('&Quit', self)
        quitAction.setShortcut('Ctrl+Q')
        quitAction.setStatusTip('Exit application')
        quitAction.setIcon(QIcon(":/quit.png"))
        self.connect(quitAction, SIGNAL('triggered()'), SLOT('close()'))

        try:
            settings = QSettings()
            altColor = settings.value("alternativeColor").toString()
            mainColor = settings.value("backgroundColor").toString()
            fromIndex = settings.value("currencyFrom").toInt()[0]
            toIndex = settings.value("currencyTo").toInt()[0]
            autoUpdate = settings.value("autoUpdate").toBool()
            self.converterGui = ConverterGui(autoUpdate, fromIndex,  toIndex)
            self.converterGui.setUserColors(mainColor,  altColor)
        except Exception,  e:
            print "Settings file not found, using defaults."
            self.converterGui = ConverterGui()
        self.setCentralWidget(self.converterGui)

        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu('&File')
        fileMenu.addAction(self.converterGui.updateAction)
        fileMenu.addAction(optionsAction)
        fileMenu.addAction(quitAction)
        helpItem = menuBar.addMenu('&Help')
        helpItem.addAction(aboutAction)
        helpItem.addAction(aboutQtAction)
        self.setWindowTitle("QCurrency Converter")

    def closeEvent(self, event):
        settings = QSettings()
        settings.setValue("autoUpdate", QVariant(self.converterGui.autoUpdate()))
        settings.setValue("currencyFrom", QVariant(self.converterGui.fromListBox.currentIndex()))
        settings.setValue("currencyTo",  QVariant(self.converterGui.toListBox.currentIndex()))
        settings.setValue("backgroundColor",  QVariant(self.converterGui.ratesTable.palette().base().color().name()))
        settings.setValue("alternativeColor",  QVariant(self.converterGui.ratesTable.palette().alternateBase().color().name()))

    def showOptions(self):
        altColor = self.converterGui.ratesTable.palette().alternateBase().color()
        mainColor = self.converterGui.ratesTable.palette().base().color()
        autoUpdate = self.converterGui.autoUpdate()
        self.optionState = OptionState(autoUpdate,  altColor,  mainColor)
        od = OptionsDialog(self.optionState,  self)
        self.connect(od,  SIGNAL("optionsApplied"),  self.applyOptions)
        od.exec_()
    
    def applyOptions(self):
        self.converterGui.setAutoUpdate(self.optionState.autoUpdate())
        self.converterGui.setUserColors(
                         self.optionState.mainColor().name(), 
                         self.optionState.altColor().name())

    def showAbout(self):
        ad = AboutDialog(__version__,  self)
        ad.exec_()

# *********************************************************************************

def main():
    app = QApplication(sys.argv)
    app.setOrganizationName("qcurrency")
    app.setApplicationName("qcurrency")

    form = MainWindow()
    form.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
